﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static VeiculoVO getVeiculo(string placa)
        {
            StringBuilder query = new StringBuilder();
            string where = "WHERE ";

            query.AppendLine("SELECT Placa, IdCor, IdMarca,  IdModelo,  IdPorte, IdTipo, Obs ");
            query.AppendLine("FROM veiculo ");

            if (placa != null)
            {
                query.AppendLine(where + "Placa=?Placa ");
                where = " AND ";
            }

            query.AppendLine("ORDER BY Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (placa != null) cmd.Parameters.AddWithValue("Placa", placa);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                VeiculoVO veiculoVO = null;

                if (reader.Read())
                {
                    veiculoVO = VeiculoVO.CreateVeiculoVO();
                    veiculoVO.placa = reader.GetString("Placa");
                    veiculoVO.idCor = reader.GetInt32("IdCor");
                    veiculoVO.idMarca = reader.GetInt32("IdMarca");
                    veiculoVO.idModelo = reader.GetInt32("IdModelo");
                    veiculoVO.idPorte = reader.GetInt32("IdPorte");
                    veiculoVO.idTipo = reader.GetInt32("IdTipo");
                    veiculoVO.obs = reader.GetString("Obs");

                }
                reader.Close();
                return veiculoVO;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setVeiculo(VeiculoVO veiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE veiculo ");
            query.AppendLine("SET IdCor=?IdCor, IdMarca=?IdMarca, IdModelo=?IdModelo, IdPorte=?IdPorte, IdTipo=?IdTipo, Obs=?Obs ");
            query.AppendLine("WHERE Placa=?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoVO.placa);
            cmd.Parameters.AddWithValue("?IdCor", veiculoVO.idCor);
            cmd.Parameters.AddWithValue("?IdMarca", veiculoVO.idMarca);
            cmd.Parameters.AddWithValue("?IdModelo", veiculoVO.idModelo);
            cmd.Parameters.AddWithValue("?IdPorte", veiculoVO.idPorte);
            cmd.Parameters.AddWithValue("?IdTipo", veiculoVO.idTipo);
            cmd.Parameters.AddWithValue("?Obs", veiculoVO.obs);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newVeiculo(VeiculoVO veiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO veiculo ");
            query.AppendLine("SET Placa=?Placa, IdCor=?IdCor, IdMarca=?IdMarca, IdModelo=?IdModelo, IdPorte=?IdPorte, IdTipo=?IdTipo, Obs=?Obs ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoVO.placa);
            cmd.Parameters.AddWithValue("?IdCor", veiculoVO.idCor);
            cmd.Parameters.AddWithValue("?IdMarca", veiculoVO.idMarca);
            cmd.Parameters.AddWithValue("?IdModelo", veiculoVO.idModelo);
            cmd.Parameters.AddWithValue("?IdPorte", veiculoVO.idPorte);
            cmd.Parameters.AddWithValue("?IdTipo", veiculoVO.idTipo);
            cmd.Parameters.AddWithValue("?Obs", veiculoVO.obs);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delVeiculo(VeiculoVO veiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM veiculo ");
            query.AppendLine("WHERE Placa=?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoVO.placa);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }
    }
}
